


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: QueryBuilderDatabaseDataStore</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.datastore.database</a> ]
</div>

<h1>Coverage Summary for Class: QueryBuilderDatabaseDataStore (at.schauer.gregor.vienna.datastore.database)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">QueryBuilderDatabaseDataStore</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (10/ 10)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (46/ 46)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.datastore.database;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.context.ViennaContext;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.datastore.StorageMetadata;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.datastore.database.querybuilder.QueryBuilder;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.serializer.Serializer;
<i>22</i>&nbsp;import org.springframework.jdbc.core.ColumnMapRowMapper;
<i>23</i>&nbsp;import org.springframework.jdbc.core.JdbcTemplate;
<i>24</i>&nbsp;
<i>25</i>&nbsp;import java.util.ArrayList;
<i>26</i>&nbsp;import java.util.Collection;
<i>27</i>&nbsp;import java.util.List;
<i>28</i>&nbsp;import java.util.Map;
<i>29</i>&nbsp;
<i>30</i>&nbsp;/**
<i>31</i>&nbsp; * @author Gregor Schauer
<i>32</i>&nbsp; */
<b class="fc"><i>33</i>&nbsp;public class QueryBuilderDatabaseDataStore&lt;T&gt; extends AbstractDatabaseDataStore&lt;T, Map&lt;String, Object&gt;&gt; {</b>
<b class="fc"><i>34</i>&nbsp;	protected ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();</b>
<i>35</i>&nbsp;	protected QueryBuilder queryBuilder;
<i>36</i>&nbsp;
<i>37</i>&nbsp;	public QueryBuilderDatabaseDataStore(ViennaContext ctx, JdbcTemplate template, QueryBuilder queryBuilder) {
<b class="fc"><i>38</i>&nbsp;		super(ctx, template);</b>
<b class="fc"><i>39</i>&nbsp;		this.queryBuilder = queryBuilder;</b>
<b class="fc"><i>40</i>&nbsp;	}</b>
<i>41</i>&nbsp;
<i>42</i>&nbsp;	@Override
<i>43</i>&nbsp;	protected &lt;E extends T&gt; Map&lt;String, Object&gt; read(StorageMetadata&lt;E&gt; metadata) {
<b class="fc"><i>44</i>&nbsp;		createEntityIfNecessary(metadata);</b>
<i>45</i>&nbsp;
<b class="fc"><i>46</i>&nbsp;		String sql = queryBuilder.getQuery(QueryBuilder.READ);</b>
<b class="fc"><i>47</i>&nbsp;		Object[] parameters = queryBuilder.getParameters(QueryBuilder.READ, metadata);</b>
<b class="fc"><i>48</i>&nbsp;		Map&lt;String, Object&gt; map = template.queryForObject(sql, rowMapper, parameters);</b>
<i>49</i>&nbsp;
<b class="fc"><i>50</i>&nbsp;		map.put(Serializer.CLASS, metadata.type.getName());</b>
<b class="fc"><i>51</i>&nbsp;		return map;</b>
<i>52</i>&nbsp;	}
<i>53</i>&nbsp;
<i>54</i>&nbsp;	@Override
<i>55</i>&nbsp;	protected &lt;E extends T&gt; E map(Map&lt;String, Object&gt; data, StorageMetadata&lt;E&gt; metadata) {
<b class="fc"><i>56</i>&nbsp;		Serializer&lt;E&gt; serializer = ctx.getSerializer(metadata.type);</b>
<b class="fc"><i>57</i>&nbsp;		return serializer.load(data);</b>
<i>58</i>&nbsp;	}
<i>59</i>&nbsp;
<i>60</i>&nbsp;	@Override
<i>61</i>&nbsp;	protected Object insert(StorageMetadata&lt;T&gt; metadata) {
<b class="fc"><i>62</i>&nbsp;		return execute(QueryBuilder.INSERT, metadata);</b>
<i>63</i>&nbsp;	}
<i>64</i>&nbsp;
<i>65</i>&nbsp;	@Override
<i>66</i>&nbsp;	protected void update(StorageMetadata&lt;T&gt; metadata) {
<b class="fc"><i>67</i>&nbsp;		execute(QueryBuilder.UPDATE, metadata);</b>
<b class="fc"><i>68</i>&nbsp;	}</b>
<i>69</i>&nbsp;
<i>70</i>&nbsp;	protected Object execute(String key, StorageMetadata&lt;T&gt; metadata) {
<b class="fc"><i>71</i>&nbsp;		createEntityIfNecessary(metadata);</b>
<i>72</i>&nbsp;
<b class="fc"><i>73</i>&nbsp;		Serializer&lt;T&gt; serializer = ctx.getSerializer(metadata.type);</b>
<b class="fc"><i>74</i>&nbsp;		Map&lt;String, Object&gt; map = serializer.save(metadata.object);</b>
<b class="fc"><i>75</i>&nbsp;		map.put(metadata.identifierName, metadata.identifier);</b>
<i>76</i>&nbsp;
<b class="fc"><i>77</i>&nbsp;		String sql = queryBuilder.getQuery(key);</b>
<b class="fc"><i>78</i>&nbsp;		Object[] parameters = queryBuilder.getParameters(key, map);</b>
<b class="fc"><i>79</i>&nbsp;		template.update(sql, parameters);</b>
<i>80</i>&nbsp;
<b class="fc"><i>81</i>&nbsp;		return metadata.identifier;</b>
<i>82</i>&nbsp;	}
<i>83</i>&nbsp;
<i>84</i>&nbsp;	@Override
<i>85</i>&nbsp;	public void delete(T obj) {
<b class="fc"><i>86</i>&nbsp;		StorageMetadata&lt;T&gt; metadata = StorageMetadata.create(ctx, obj);</b>
<b class="fc"><i>87</i>&nbsp;		createEntityIfNecessary(metadata);</b>
<i>88</i>&nbsp;
<b class="fc"><i>89</i>&nbsp;		String sql = queryBuilder.getQuery(QueryBuilder.DELETE);</b>
<b class="fc"><i>90</i>&nbsp;		Object[] parameters = queryBuilder.getParameters(QueryBuilder.DELETE, metadata);</b>
<b class="fc"><i>91</i>&nbsp;		template.update(sql, parameters);</b>
<b class="fc"><i>92</i>&nbsp;	}</b>
<i>93</i>&nbsp;
<i>94</i>&nbsp;	@Override
<i>95</i>&nbsp;	public &lt;E extends T&gt; Collection&lt;E&gt; list(Class&lt;E&gt; type) {
<b class="fc"><i>96</i>&nbsp;		StorageMetadata&lt;E&gt; metadata = StorageMetadata.create(ctx, type);</b>
<b class="fc"><i>97</i>&nbsp;		createEntityIfNecessary(metadata);</b>
<i>98</i>&nbsp;
<b class="fc"><i>99</i>&nbsp;		String sql = queryBuilder.getQuery(QueryBuilder.LIST);</b>
<b class="fc"><i>100</i>&nbsp;		Object[] parameters = queryBuilder.getParameters(QueryBuilder.LIST, metadata);</b>
<b class="fc"><i>101</i>&nbsp;		List&lt;Map&lt;String, Object&gt;&gt; maps = template.queryForList(sql, parameters);</b>
<i>102</i>&nbsp;
<b class="fc"><i>103</i>&nbsp;		List&lt;E&gt; objects = new ArrayList&lt;E&gt;(maps.size());</b>
<b class="fc"><i>104</i>&nbsp;		for (Map&lt;String, Object&gt; map : maps) {</b>
<b class="fc"><i>105</i>&nbsp;			map.put(Serializer.CLASS, metadata.type.getName());</b>
<b class="fc"><i>106</i>&nbsp;			metadata.identifier = map.get(metadata.identifierName);</b>
<b class="fc"><i>107</i>&nbsp;			objects.add(map(map, metadata));</b>
<i>108</i>&nbsp;		}
<b class="fc"><i>109</i>&nbsp;		return objects;</b>
<i>110</i>&nbsp;	}
<i>111</i>&nbsp;
<i>112</i>&nbsp;	protected void createEntityIfNecessary(StorageMetadata&lt;? extends T&gt; metadata) {
<b class="fc"><i>113</i>&nbsp;		if (!getTables().contains(metadata.entityName)) {</b>
<b class="fc"><i>114</i>&nbsp;			String sql = queryBuilder.getQuery(QueryBuilder.CREATE);</b>
<b class="fc"><i>115</i>&nbsp;			template.execute(sql);</b>
<b class="fc"><i>116</i>&nbsp;			getTables().add(metadata.entityName);</b>
<i>117</i>&nbsp;		}
<b class="fc"><i>118</i>&nbsp;	}</b>
<i>119</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:22</div>
</div>
</body>
</html>
